package com.akk.platform.oem;

import com.akk.module.app.entity.AppBanner;
import com.akk.module.app.model.request.AppBannerRequest;
import com.akk.module.app.service.AppBannerService;
import com.akk.module.upload.service.PictureService;
import com.akk.utils.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/platform/oem/appBanner")
public class AppBannerAction {

    @Autowired
    private AppBannerService appBannerService;

    @Autowired
    private PictureService pictureServices;

    @RequestMapping("/list")
    @RequiresPermissions("platform:oem:banner:list")
    public RestResponse findAll(Query query){
        Map<String,Object> param = new HashMap<>();
        param.put("appId", ShiroUtils.getAdmin().getAppId());
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<AppBanner> list = appBannerService.findByParam(param);
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    @RequestMapping("/app/{appId}")
    @RequiresPermissions("platform:oem:banner:app")
    public RestResponse queryByAppId(@PathVariable String appId){
        Map<String,Object> param = new HashMap<>();
        param.put("appId", appId);
        List<AppBanner> list = appBannerService.findByParam(param);
        return RestResponse.build(list);
    }

    @RequestMapping("/info/{bannerId}")
    @RequiresPermissions("platform:oem:banner:info")
    public RestResponse queryById(@PathVariable String id){
        return RestResponse.build(appBannerService.findById(id));
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @RequiresPermissions("platform:oem:banner:save")
    public RestResponse save(AppBannerRequest request){
        if (request.getBannerImage() == null)
            return RestResponse.build("1","请上传一张banner图片");
        Map<String,Object> map = pictureServices.uploadPicture(request.getBannerImage());
        if (map.get("status").toString().equals("1"))
            return RestResponse.build("1","图片上传失败，请重试");
        String imageUrl = map.get("url").toString();
        AppBanner banner = new AppBanner();
        banner.setBannerId(IDUtils.getPK());
        banner.setAppId(ShiroUtils.getAdmin().getAppId());
        banner.setBannerTag(request.getBannerTag());
        banner.setBannerDesc(request.getBannerDesc());
        banner.setBannerImg(imageUrl);
        banner.setBannerUrl(request.getBannerUrl());
        banner.setBannerTitle(request.getBannerTitle());
        appBannerService.save(banner);
        return RestResponse.build("success");
    }

    @PostMapping(value = "/update")
    @RequiresPermissions("platform:oem:banner:update")
    public RestResponse update(AppBannerRequest request){
        if (request.getBannerId() == null || request.getBannerId().length() < 1)
            return RestResponse.build("1","参数错误");
        AppBanner banner = appBannerService.findById(request.getBannerId());
        banner.setBannerTitle(request.getBannerTitle());
        banner.setBannerDesc(request.getBannerDesc());
        banner.setBannerUrl(request.getBannerUrl());
        banner.setBannerTag(request.getBannerTag());
        banner.setAppId(ShiroUtils.getAdmin().getAppId());
        if (request.getBannerImage() != null) {
            Map map = pictureServices.uploadPicture(request.getBannerImage());
            if (map.get("status").toString().equals("1"))
                return RestResponse.build("1","图片上传失败，请重试");
            String fileUrl = banner.getBannerImg();
            if (!StringUtils.isEmpty(fileUrl)){
                fileUrl = fileUrl.substring(fileUrl.indexOf("/picture/")+1,fileUrl.length());
                pictureServices.removeFile(fileUrl);
            }
            banner.setBannerImg(map.get("url").toString());
        }
        appBannerService.update(banner);
        return RestResponse.build("success");
    }

    @RequestMapping("/delete/{bannerId}")
    @RequiresPermissions("platform:oem:banner:delete")
    public RestResponse delete(@PathVariable String bannerId){
        AppBanner banner1 = appBannerService.findById(bannerId);
        if (banner1!=null){
            String fileUrl1 = banner1.getBannerImg();
            if (!StringUtils.isEmpty(fileUrl1)){
                fileUrl1 = fileUrl1.substring(fileUrl1.indexOf("/picture/")+1,fileUrl1.length());
                pictureServices.removeFile(fileUrl1);
            }
        }
        if (bannerId == null || bannerId.length() < 1)
            return RestResponse.build("1","参数错误");
        appBannerService.delete(bannerId);
        return RestResponse.build("success");
    }

}
